package com.ruoyi.system.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.web.domain.server.Sys;
import com.ruoyi.system.domain.AdminDivisionModel;
import com.ruoyi.system.service.*;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@Api(value = "用户登录认证", tags = { "用户登录认证" })
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private AdminDivisionService adminDivisionService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private LabelAreaService labelAreaService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) throws Exception {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        LoginUser loginUser =  loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());

        String token = tokenService.createToken(loginUser);
//        System.out.println("22222"+loginUser.getLabelAreas2());
        String str  = loginUser.getLabelAreas2().get(0);
        QueryWrapper<AdminDivisionModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("RG_NAME", str);
        System.out.println(queryWrapper);
        if(queryWrapper ==null){
            queryWrapper.like("RG_NAME", "北京");
            str = "北京";
        }
        AdminDivisionModel adminDivisionModel= adminDivisionService.getOne(queryWrapper);
        System.out.println("adminDivisionmodel"+adminDivisionModel);
        System.out.println(loginUser);
        double lon = adminDivisionModel.getRgCenterX();
        double lat = adminDivisionModel.getRgCenterY();
//        int scaleLevel = 20;
//        labelAreaService.updateLabelArea(lon,lat,scaleLevel,str);

        ajax.put(Constants.TOKEN, token);
        ajax.put("realname", loginUser.getUser().getRealName());
        ajax.put("deptName", loginUser.getUser().getDept().getDeptName());
        ajax.put("lon", lon);
        ajax.put("lat", lat);
        //等数据库完善
//        ajax.put("lon", loginUser.getUser().getLon());
//        ajax.put("lat", loginUser.getUser().getLat());
        ajax.put("scale_level", loginUser.getUser().getScale_level());


        ajax.put("roles",loginUser.getUser().getRoles().stream().map(SysRole::getRoleName).collect(Collectors.toList()));
        ajax.put("areaLabels",loginUser.getLabelAreas());
        ajax.put("busLabels",loginUser.getLabelBuses());
        ajax.put("deptName", loginUser.getUser().getDept().getDeptName());
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @ApiOperation("获取路由信息")
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
